package org.yuxiong.utils;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.commons.httpclient.NameValuePair;


public class Utils {
	
	public static String getDomainByURL(String url){
		return getDomainByURL(url, ".com");
	}
	
	public static String getDomainByURL(String url, String domainPrifex){
		return url.substring(0, url.indexOf(domainPrifex)+4);
	}
	
	/**
	 * 获取本地的IP地址
	 * @return
	 */
	public static String getLocalIP(){
		try   {  
			  String hostName=InetAddress.getLocalHost().getHostName();  
			  InetAddress[] address=InetAddress.getAllByName(hostName);  
			  if(address!=null) return address[0].getHostAddress();
			 
		}catch(Exception   e)   {  
			e.printStackTrace();  
		}
		return null;
	}
	
	/**
	 * 取得指定范围的随机数
	 * @return
	 */
	public static int randNumber(int min, int max){
		Random random = new Random(); 
		return random.nextInt(max)%(max-min+1) + min; 
	}

	public static String toString(Map map){
		if(map!=null){
			StringBuffer sb = new StringBuffer();
			Set set = map.entrySet(); 
			for(Iterator it = set.iterator();it.hasNext();){
				Map.Entry me = (Map.Entry)it.next(); 
				sb.append(me.getKey()).append("=").append(me.getValue());
				if(it.hasNext()) sb.append("&");
			}
			return sb.toString();
		}
		return "";
	}
	
	public static String toString(List<NameValuePair> pair){
		if(pair!=null){
			StringBuffer sb = new StringBuffer();
			
			for(Iterator it = pair.iterator();it.hasNext();){
				NameValuePair nvp = (NameValuePair)it.next(); 
				sb.append(nvp.getName()).append("=").append(nvp.getValue());
				if(it.hasNext()) sb.append("&");
			}
			return sb.toString();
		}
		return "";
	}
	
	public static Map params2Map(String params){
		Map rtnMap = null;
		if(params!=null){
			rtnMap = new HashMap();
			String[] paramsArr = params.split("&");
			for(int i=0;i<paramsArr.length;i++){
				
				String[] paramArr = paramsArr[i].split("=");
				if(paramArr.length == 2)
					rtnMap.put(paramArr[0], paramArr[1]);
				else
					rtnMap.put(paramArr[0], null);
			}
		}
		return rtnMap;
	}
	
	public static Map params2MapByURL(String url){
		if(url!=null){
			return params2Map(url.split("\\?")[1]);
		}
		return null;
	}
	
	public static void debug(Object o){
		System.out.println(o);
	}
	public static void debug(Object[] al){
		System.out.println("--------------- DEBUG Object[] START "+new SuperDate().getDateTimeString()+" -------------------");
		if(al != null && al.length > 0) { 
				System.out.println("al.length="+al.length);
			   for(int i = 0; i < al.length; i ++) {
				   printDebug(al[i]);
			   }
		}else{
			System.out.println("Object[] is null");
		}
		System.out.println("--------------- DEBUG Object[] END "+new SuperDate().getDateTimeString()+" -------------------\n");
	}
	
	public static void debug(byte[] al){
		System.out.println("--------------- DEBUG byte[] START "+new SuperDate().getDateTimeString()+" -------------------");
		if(al != null && al.length > 0) { 
				System.out.println("al.length="+al.length);
			   for(int i = 0; i < al.length; i ++) {
				   printDebug(al[i]);
			   }
		}else{
			System.out.println("byte[] is null");
		}
		System.out.println("--------------- DEBUG byte[] END "+new SuperDate().getDateTimeString()+" -------------------\n");
	}
	
	public static void debug(List al){
		System.out.println("--------------- DEBUG ArrayList START "+new SuperDate().getDateTimeString()+" -------------------");
		if(al != null && al.size() > 0) { 
				System.out.println("ArrayList.size()="+al.size());
			   for(int i = 0; i < al.size(); i ++) {
				   printDebug(al.get(i));
			   }
		}else{
			System.out.println("ArrayList is null");
		}
		System.out.println("--------------- DEBUG ArrayList END "+new SuperDate().getDateTimeString()+" -------------------\n");
	}
	
	public static void debug(Set set){
		System.out.println("--------------- DEBUG Set START "+new SuperDate().getDateTimeString()+" -------------------");
		if(set != null && set.size() > 0) { 
				System.out.println("Set.size()="+set.size());
				for(Iterator it = set.iterator();it.hasNext();){
					printDebug(it.next());
				}
		}else{
			System.out.println("Set is null");
		}
		System.out.println("--------------- DEBUG Set END "+new SuperDate().getDateTimeString()+" -------------------\n");
	}
	
	public static void debug(Map map){
		System.out.println("--------------- DEBUG Map START "+new SuperDate().getDateTimeString()+" -------------------");
		Set set = map.entrySet(); 
		for(Iterator it = set.iterator();it.hasNext();){
			Map.Entry me = (Map.Entry)it.next(); 
			System.out.println("Key = " + me.getKey());
			printDebug(me.getValue());
		}
		System.out.println("--------------- DEBUG Map END "+new SuperDate().getDateTimeString()+" -------------------\n");
	}
	
	public static void debug(Enumeration enumer){
		System.out.println("--------------- DEBUG Map START "+new SuperDate().getDateTimeString()+" -------------------");
		if(enumer != null) {
			 while (enumer.hasMoreElements()) {
		         printDebug(enumer.nextElement());
		     }

		}else{
			System.out.println("Set is null");
		}
		System.out.println("--------------- DEBUG Map END "+new SuperDate().getDateTimeString()+" -------------------\n");
	}
	
	private static void printDebug(Object obj){
		if(obj instanceof Object[]) 
			debug((Object[])obj); 
		   else{
			   System.out.println(obj);
		   }
	}
}
